using Godot;
using NLua;
using System;
using System.Collections.Generic;
public class BlockMaterial : SWMaterial
{
    public ImageSet imageset;
    public BlockShape.BlockShapeEnum shape = BlockShape.BlockShapeEnum.Block;
    public bool hide = false;
    public Lua script;
    public Texture texture;
    public int id;
    public ItemMateria ItemMateria;

    public delegate BlockEntity CreateBlockEntityEvent();
    public delegate void OnRotateBlockEvent(PlayerRotateBlockEvent e);
    public delegate void OnTickEvent(UpdataTickEvent e);
    public delegate bool OnUseEvent(PlayerUseBlockEvent e);
    public delegate bool OnBreakEvent(PlayerBreakBlockEvent e);

    public string Group = "";
    public float Durable = 1;
    public bool CanBreak = true;
    public bool CanReplace = false;
    public int StateMax = 0;
    public int RotateMax = 0;
    public int type = 0;
    public int Fule = 0;
    public bool redstone = false;
    public bool Collide = true;
    public bool View = false;
    public string Alias;
    public StoreView OpenView = null;
    public Tool tool = new Tool(ToolType.Hand, 0);
    public List<DropItem> dropItems = new List<DropItem>();
    public CreateBlockEntityEvent CreateBlockEntity;
    public OnTickEvent UpdataBlockEvent;
    public OnTickEvent TickEvent;
    public OnTickEvent RedstoneTick;
    public OnUseEvent UseEvent;
    public OnBreakEvent BreakEvent;
    public OnRotateBlockEvent RotateEvent;

    public BlockMaterial(string name) : base(name, MaterialType.Block)
    {
        Alias = name;
        texture = (Texture)GD.Load($"res://Texture/Block/{name}.png");
        if (texture == null) texture = (Texture)GD.Load($"res://Texture/Block/{name}_0.png");
        UseEvent = OnUse;
    }
    public BlockMaterial SetImageSet(ImageSet imageSet)
    {
        this.imageset = imageSet;
        return this;
    }
    public BlockMaterial SetDisplayName(string DisPlayName)
    {
        this.DisPlayName = DisPlayName;
        return this;
    }
    public BlockMaterial SetCollide(bool f)
    {
        Collide = f;
        return this;
    }
    public BlockMaterial SetView(bool v)
    {
        this.View = v;
        return this;
    }
    public BlockMaterial SetStateMax(int s)
    {
        StateMax = s;
        return this;
    }
    public BlockMaterial SetBlockEntityCreate(CreateBlockEntityEvent e)
    {
        this.CreateBlockEntity = e;
        return this;
    }
    public BlockMaterial SetTickEvent(OnTickEvent e)
    {
        this.TickEvent = e;
        return this;
    }
    public BlockMaterial AddDrop(String name, int amount)
    {
        dropItems.Add(new DropItem(name, amount));
        return this;
    }
    public List<ItemStack> GetDrop(int amount = 1)
    {
        List<ItemStack> items = new List<ItemStack>();
        if (this.dropItems.Count == 0)
        {
            items.Add(GetItemStack(amount));
            return items;
        }
        else
        {
            for (int i = 0; i < dropItems.Count; i++)
            {
                ItemStack item = new ItemStack(Materials.GetItemMateria(dropItems[i].Name), dropItems[i].Amount);
                items.Add(item);
            }
            return items;
        }
    }
    public ItemStack GetItemStack(int amount = 1)
    {
        return new ItemStack(this.GetItemMateria(), amount);
    }
    public ItemMateria GetItemMateria()
    {
        return ItemMateria;
    }


    public virtual bool OnUse(PlayerUseBlockEvent e)
    {
        return false;
    }

    public virtual int GetTileId(BlockData block)
    {
        return id + block.State + block.Rotate;
    }
}

public class DropItem
{
    public String Name;
    public int Amount;
    public DropItem(String Name, int Amount)
    {
        this.Name = Name;
        this.Amount = Amount;
    }
}